//
//  CarDetailViewController.m
//  Car
//
//  Created by lanou on 15/9/19.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "CarDetailViewController.h"


@interface CarDetailViewController ()<UIWebViewDelegate>
@property (nonatomic ,strong)UIWebView *webView;
@property (nonatomic ,strong)NSString *webUrl;

@end

@implementation CarDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [ProgressHUD show:@"正在加载..."];
    
    [self reloadAndJosn];
    // Do any additional setup after loading the view.
}

- (void)reloadAndJosn{
    if (self.num == 1) {
        [LORequestManger GET:[NSString stringWithFormat: @"http://app.api.autohome.com.cn/autov4.8.8/2sc/2scinfo-pm1-cid%@.json",self.url] success:^(id response) {
            NSDictionary *dic = (NSDictionary *)response;
            self.webUrl = dic[@"result"][@"shareinfo"][@"url"];
            [self setUpData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }else
    {
    [LORequestManger GET:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.8.8/cars/seriessummary-pm1-s%@-t-c320400.json",self.url] success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        self.webUrl = dic[@"result"][@"shareinfo"][@"url"];
        [self setUpData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
        
    }
    
}

- (void)setUpData{
    
    self.webView = [[UIWebView alloc]initWithFrame:kMainScreen];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]];
    [self.webView loadRequest:request];

}

- (void)viewDidAppear:(BOOL)animated{
	[super viewDidAppear:YES];
    for (UIView *views in self.navigationController.navigationBar.subviews) {
        if ([views isKindOfClass:[UISegmentedControl class]]) {
            [views setHidden:YES];
        }
    }
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //删除header
    NSMutableString *js0 = [NSMutableString string];
    [js0 appendString:@"var post = document.getElementsByClassName('header-2sc')[0];"];
    [js0 appendString:@"post.parentNode.removeChild(post);"];
    
    [webView stringByEvaluatingJavaScriptFromString:js0];
    
    [ProgressHUD dismiss];
}
- (void)viewWillDisappear:(BOOL)animated{
    [ProgressHUD dismiss];
}

@end
